// pages/mine/collection/index.js
var request = require("../../../utils/request.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    course: [],
    pageNum: 1,
    flag: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  init:function(){
    var url = "api/Customer/GetMyCollections";
    this.getList(url,[])
  },

  getList(url, data) {
    var that = this;

    data['pageIndex'] = that.data.pageNum;

    data['id'] = app.globalData.userInfo['ID'];

    console.log(url, data);

    request.request(url, data).then(function (res) {

      console.log(res)
      var result = res.data;

      wx.showLoading({
        title: '加载中',
      })

      if (result.status == 1) {

        for (var i = 0; i < result.data.length; i++) {
          var pic = result.data[i]['Pic'].split(',')

          // for (var j in pic) {
          //   pic[j] = "http://qbonbon.qclub-online.com/_Document/Image/" + pic[j]

          // }

          result.data[i]['Pic'] = pic[0];

        }

        var data = that.data.course;

        if (result.data.length > 0) {
          for (var i in result.data) {
            data.push(result.data[i]);
          }
        }
        else {

          if (that.data.pageNum == 0) {
            data = result.data;
          } else {
            that.setData({
              flag: false
            })
          }
        }

        that.setData({
          "course": data
        })
        wx.hideLoading();
      }
    });
  }
})